// pages/my_resume/my_resume.js
const app = getApp(), core = require('../../utils/core');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    resume: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(!app.getUserinfo(true)) {
      return;
    }
    // if(userinfo.group_id!=1) {
    //   wx.redirectTo({url: '/pages/change/change'});
    //   wx.showToast({title: '您需要换到个人用户身份使用此功能',icon:'none'});
    //   return;
    // }
    this.refresh();
    app.eventBus.on(app.Events.UserLogin, this.eventOnUserLogin, this);
  },

  onUnload() {
    app.eventBus.off(app.Events.UserLogin, this.eventOnUserLogin);
  },

  eventOnUserLogin() {
    this.refresh();
  },

  refresh() {
    core.get({
      url: '/xilujob.user/my_resume',
      success: (ret, response) => {
        this.setData({resume: ret.data.resume});
      }
    });
  },

  bindEditPersonalInfo() {
    wx.navigateTo({
      url: 'personal_info',
      events: {
        dataTransfer: data => {
          this.setData({'resume.personal': data.model, 'resume.complete_percent': data.complete_percent});
          app.eventBus.emit(app.Events.UcenterInfoCompleteChange, data.complete_percent);
        }
      },
      success: res => {
        res.eventChannel.emit('dataTransfer', {model:this.data.resume.personal});
      }
    });
  },

  // bindEditJobIntention() {
  //   wx.navigateTo({
  //     url: 'job_intention',
  //     events: {
  //       dataTransfer: data => {
  //         this.setData({'resume.job_intention': data.model, 'resume.complete_percent': data.complete_percent});
  //         app.eventBus.emit(app.Events.UcenterInfoCompleteChange, data.complete_percent);
  //       }
  //     },
  //     success: res => {
  //       res.eventChannel.emit('dataTransfer', {model: this.data.resume.job_intention});
  //     }
  //   });
  // },

  bindEditJobIntention(event) {
    wx.navigateTo({
      url: 'job_intention',
      events: {
        dataTransfer: data => {
          let job_intention = data.model, index = null, job_intentions = this.data.resume.job_intention;
          for(let i=0;i<job_intentions.length;i++) {
            if(job_intentions[i].id==job_intention.id) {
              index = i;
              break;
            }
          }
          if(index===null) {
            job_intentions.push(job_intention);
          }else {
            job_intentions.splice(index, 1, job_intention);
          }
          job_intentions.sort((a,b)=>{
            return a.start > b.start ? -1 : 1;
          });
          this.setData({'resume.job_intention': job_intentions, 'resume.complete_percent': data.complete_percent});
          app.eventBus.emit(app.Events.UcenterInfoCompleteChange, data.complete_percent);
        }
      },
      success: res => {
        res.eventChannel.emit('dataTransfer', {model: event.currentTarget.dataset.model||{}});
      }
    });
  },

  bindEditEducation(event) {
    wx.navigateTo({
      url: 'education',
      events: {
        dataTransfer: data => {
          let education = data.model, index = null, educations = this.data.resume.educations;
          for(let i=0;i<educations.length;i++) {
            if(educations[i].id==education.id) {
              index = i;
              break;
            }
          }
          if(index===null) {
            educations.push(education);
          }else {
            educations.splice(index, 1, education);
          }
          educations.sort((a,b)=>{
            return a.start > b.start ? -1 : 1;
          });
          this.setData({'resume.educations': educations, 'resume.complete_percent': data.complete_percent});
          app.eventBus.emit(app.Events.UcenterInfoCompleteChange, data.complete_percent);
        }
      },
      success: res => {
        res.eventChannel.emit('dataTransfer', {model: event.currentTarget.dataset.model||{}});
      }
    });
  },

  bindEditSkill(event) {
    wx.navigateTo({
      url: 'skill',
      events: {
        dataTransfer: data => {
          let skill = data.model, index = null, skills = this.data.resume.skills;
          for(let i=0;i<skills.length;i++) {
            if(skills[i].id==skill.id) {
              index = i;
              break;
            }
          }
          if(index===null) {
            skills.push(skill);
          }else {
            skills.splice(index, 1, skill);
          }
          this.setData({'resume.skills': skills, 'resume.complete_percent': data.complete_percent});
          app.eventBus.emit(app.Events.UcenterInfoCompleteChange, data.complete_percent);
        }
      },
      success: res => {
        res.eventChannel.emit('dataTransfer', {model: event.currentTarget.dataset.model||{}});
      }
    });
  },

  bindEditWork(event) {
    wx.navigateTo({
      url: 'work',
      events: {
        dataTransfer: data => {
          let work = data.model, index = null, works = this.data.resume.works;
          for(let i=0;i<works.length;i++) {
            if(works[i].id==work.id) {
              index = i;
              break;
            }
          }
          if(index===null) {
            works.push(work);
          }else {
            works.splice(index, 1, work);
          }
          works.sort((a,b)=>{
            return a.start > b.start ? -1 : 1;
          });
          this.setData({'resume.works': works, 'resume.complete_percent': data.complete_percent});
          app.eventBus.emit(app.Events.UcenterInfoCompleteChange, data.complete_percent);
        }
      },
      success: res => {
        res.eventChannel.emit('dataTransfer', {model: event.currentTarget.dataset.model||{}});
      }
    });
  },

  bindEditSummary() {
    wx.navigateTo({
      url: 'sum_up',
      events: {
        dataTransfer: data => {
          this.setData({'resume.summary': data.model, 'resume.complete_percent': data.complete_percent});
          app.eventBus.emit(app.Events.UcenterInfoCompleteChange, data.complete_percent);
        }
      },
      success: res => {
        res.eventChannel.emit('dataTransfer', {model:this.data.resume.summary});
      }
    });
  },

  bindEditPersonalTag() {
    wx.navigateTo({
      url: 'personal_tag',
      events: {
        dataTransfer: data => {
          this.setData({'resume.personal_tags': data.model, 'resume.complete_percent': data.complete_percent});
          app.eventBus.emit(app.Events.UcenterInfoCompleteChange, data.complete_percent);
        }
      },
      success: res => {
        res.eventChannel.emit('dataTransfer', {model: this.data.resume.personal_tags});
      }
    });
  },

  bindDelModule(event) {
    let module = event.currentTarget.dataset.module, id = event.currentTarget.dataset.id,
      listKey = event.currentTarget.dataset.list_key;//resume.$list_key , list在resume中的key
    wx.showModal({
      title: '提示',
      content: '确认删除？',
      success: res => {
        if (res.confirm) {
          core.post({
            url: 'xilujob.user/del_module', data: {module, id},
            success: ret => {
              let list = this.data.resume[listKey], index = null;
              for(let i=0;i<list.length;i++) {
                if(list[i].id==id) {
                  index = i;
                  break;
                }
              }
              if(index!==null) {
                list.splice(index, 1);
                this.setData({['resume.'+listKey]: list});
              }
              this.setData({'resume.complete_percent': ret.data.complete_percent});
              app.eventBus.emit(app.Events.UcenterInfoCompleteChange, data.complete_percent);
            }
          })
        }
      }
    })
  },

  bindViewResume() {
    wx.navigateTo({
      url: '/pages/talent_info_detail/talent_info_detail?id='+this.data.resume.id
    })
  },

   //简历附件
   bindResumeFiles(){
    wx.navigateTo({
      url: '/pages/resume_files/resume_files',
      events: {
          dataTransfer: data => {
            this.setData({'resume.resume_files': data.files});
            
          }
        }
    })
    }

});
